/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml;

import pikes.uri.RelativeURI;
import pikes.uri.URI;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;image/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/struct/objects.html#h-13.2">IMG element</a>
 * in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/struct/objects.html#h-13.2">The IMG element in HTML specification</a>
 */
public class Image extends NonRootTagFactory implements Special, CoreAttributes, I18N, Events {

	private URI uri = null;

	private CharSequence alt = null;

	private Integer height = null;

	private Integer width = null;

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public URI getUri() {
		return uri;
	}

	public void setUri(URI uri) {
		this.uri = uri;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	/**
	 * Creates an <code>&lt;image/&gt;</code> tag
	 * @param uri The URI of the image
	 * @param alt Alternative string
	 */
	public Image(URI uri, CharSequence alt) {
		Assert.notNull(uri);
		Assert.hasText(alt);
		this.uri = uri;
		this.alt = alt;
	}

	/**
	 * Creates an <code>&lt;image/&gt;</code> tag
	 * @param uri The URI of the image
	 * @param alt Alternative string
	 */
	public Image(CharSequence uri, CharSequence alt) {
		this(new RelativeURI(uri), alt);
	}

	/**
	 * @return "img"
	 */
	@Override
	final protected CharSequence getTagName() {
		return "img";
	}

	/**
	 * Adds 'src' and 'alt' attributes.
	 * @param tag the parent tag to be decorated
	 */
	@Override
	protected void decorateCreatedTag(XMLTag tag) {
		tag.addAttribute("src", uri);
		tag.addAttribute("alt", alt);

		if (height != null) {
			tag.addAttribute("height", height.toString());
		}

		if (width != null) {
			tag.addAttribute("width", width.toString());
		}
	}

}
